目录
1、回显模式
2、成果显示
3、知识点
1)FormLayout布局添加addRow方法
2)在输入框显示灰色提示字体,输入内容时消失setPlaceholderText
3)设置回显模式setEchoMode
4、完整代码
1、回显模式
QLineEdit控件的主要功能是输入一行文本内容,输入内容的过程中内容显示的方式称之为回显,支持四种回显方式。
1)正常,即输入什么显示什么(Normal)
2)不显示,用户输入什么在输入框中不显示,用户也不知道输入了什么,少用(NoEcho)
3)密码形式,即用户输入的时候,在输入框中显示的是黑点(PassWord)
4)PasswordEchoOnEdit——在编辑这个输入框的时候显示,不编辑时编程黑点
2、成果显示
开始界面
![](https://img-blog.csdnimg.cn/285a9cf6344d47f2b4de8781fac4cc04.png)
输入文本后,光标在PassWordEchoOnEdit输入框时,可以看到密码时可以看到的
![](https://img-blog.csdnimg.cn/9626783c9ffb49f0a095d4fb982c72a7.png)
光标不在PassWordEchoOnEdit输入框时,可以看到密码时不可以看到的
![](https://img-blog.csdnimg.cn/cf80e764dfae44a1bf4cd37d32a11f73.png)
注:并且再次编辑PassWordEchoOnEdit输入框时,会先将输入框的内容全部清空,然后重新输入新的内容
3、知识点
1)FormLayout布局添加addRow方法
![](https://img-blog.csdnimg.cn/faaec00bb94e41f693e98eb4d3ee7a58.png)
支持多种方法
# 将控件放入布局中
formLayout.addRow("Normal",normalLineEdit)
formLayout.addRow("noEcho",noEchoLineEdit)
formLayout.addRow("Password",PasswordLineEdit)
formLayout.addRow("PasswordEchoOnEdit",PasswordEchoOnEditLineEdit)
2)在输入框显示灰色提示字体,输入内容时消失setPlaceholderText
# 在输入框中显示灰色字体,用于提示
normalLineEdit.setPlaceholderText("Normal")
noEchoLineEdit.setPlaceholderText("noEcho")
PasswordLineEdit.setPlaceholderText("Password")
PasswordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEdit")
![](https://img-blog.csdnimg.cn/e2ef286c1c9440a78e72d0943a1c402c.png)
3)设置回显模式setEchoMode
# 设置回显模式
normalLineEdit.setEchoMode(QLineEdit.Normal)
noEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
PasswordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
PasswordLineEdit.setEchoMode(QLineEdit.Password)
4、完整代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2022/5/20 16:44
# @Author : @linlianqin
# @Site :
# @File : QLineEdit_learn.py
# @Software: PyCharm
# @description:
from PyQt5.QtWidgets import QDialog,QLabel,QLineEdit,QPushButton,QFormLayout,QApplication,QWidget
class QLineEditLearn(QWidget):
def __init__(self):
super(QLineEditLearn, self).__init__()
self.InitUI()
# 初始化界面
def InitUI(self):
self.setWindowTitle("QLineEdit文本回显学习")
# 创建多个输入框
normalLineEdit = QLineEdit()
noEchoLineEdit = QLineEdit()
PasswordLineEdit = QLineEdit()
PasswordEchoOnEditLineEdit = QLineEdit()
# 创建一个form布局
formLayout = QFormLayout()
# 将控件放入布局中
formLayout.addRow("Normal",normalLineEdit)
formLayout.addRow("noEcho",noEchoLineEdit)
formLayout.addRow("Password",PasswordLineEdit)
formLayout.addRow("PasswordEchoOnEdit",PasswordEchoOnEditLineEdit)
# 在输入框中显示灰色字体,用于提示
normalLineEdit.setPlaceholderText("Normal")
noEchoLineEdit.setPlaceholderText("noEcho")
PasswordLineEdit.setPlaceholderText("Password")
PasswordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEdit")
# 设置回显模式
normalLineEdit.setEchoMode(QLineEdit.Normal)
noEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
PasswordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
PasswordLineEdit.setEchoMode(QLineEdit.Password)
# 将布局放入到里面
self.setLayout(formLayout)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
demoWin = QLineEditLearn()
demoWin.show()
sys.exit(app.exec_())
|